package com.controller;

import com.model.FrontUser;
import com.model.vo.FrontUserVo;
import org.modelmapper.ModelMapper;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class BaseController {
    private ServletRequestAttributes getRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    public HttpSession getSession() {
        return getRequestAttributes().getRequest().getSession();
    }

    public FrontUser getFrontUser() {
        return (FrontUser) this.getSession().getAttribute("front_user");
    }

    public FrontUserVo getFrontUserVo() {
        return new ModelMapper().map(getFrontUser(), FrontUserVo.class);
    }

    public void setFrontUser(FrontUser user) {
        this.getSession().setAttribute("front_user", user);
    }

    public void removeFrontUser() {
        this.getSession().removeAttribute("front_user");
    }
}
